/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SCAN_SERVICE_ABILITY_H
#define SCAN_SERVICE_ABILITY_H

#include <mutex>
#include <string>
#include <vector>

#include "ability_manager_client.h"
#include "event_handler.h"
#include "extension_ability_info.h"
#include "iscan_callback.h"
#include "iremote_object.h"
#include "scan_constant.h"
#include "scan_service_stub.h"
#include "system_ability.h"

#include "sane/sane.h"
#include "sane/saneopts.h"
#include "scanner_info.h"
#include "scan_mdns_service.h"


namespace OHOS::Scan {
enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
class IKeyguardStateCallback;

class ScanServiceAbility : public SystemAbility, public ScanServiceStub {
    DECLARE_SYSTEM_ABILITY(ScanServiceAbility);

public:
    DISALLOW_COPY_AND_MOVE(ScanServiceAbility);
    ScanServiceAbility(int32_t systemAbilityId, bool runOnCreate);
    ScanServiceAbility();
    ~ScanServiceAbility();
    static sptr<ScanServiceAbility> GetInstance();
    
    int32_t InitScan(int32_t &scanVersion) override;
    int32_t ExitScan() override;
    int32_t GetScannerList() override;
    int32_t StopDiscover() override;
    int32_t OpenScanner(const std::string scannerId) override;
    int32_t CloseScanner(const std::string scannerId) override;
    int32_t GetScanOptionDesc(const std::string scannerId, const int32_t optionIndex, ScanOptionDescriptor &desc) override;
    int32_t OpScanOptionValue(const std::string scannerId, const int32_t optionIndex, const ScanOptionOpType op, ScanOptionValue &value, int32_t &info) override;
    int32_t GetScanParameters(const std::string scannerId, ScanParameters &para) override;
    int32_t StartScan(const std::string scannerId) override;
    int32_t GetSingleFrame(const std::string scannerId, int32_t &size, uint8_t **data) override;
    int32_t GetSingleFrameFD(const std::string scannerId, int32_t &size, uint32_t fd) override;
    int32_t CancelScan(const std::string scannerId) override;
    int32_t SetScanIOMode(const std::string scannerId, const bool isNonBlocking) override;
    int32_t GetScanSelectFd(const std::string scannerId, int32_t &fd) override;
    int32_t On(const std::string taskId, const std::string &type, const sptr<IScanCallback> &listener) override;
    int32_t Off(const std::string taskId, const std::string &type) override;
    void SendDeviceInfoTCP(const ScanDeviceInfoTCP &info,std::string event);
    void SendDeviceInfoLibUSB(const ScanDeviceInfoLibUSB &info,std::string event);
    void SendInitEvent(int32_t &scanVersion,std::string event);

protected:
    void OnStart() override;
    void OnStop() override;

private:
    int32_t ServiceInit();
    void InitServiceHandler();
    void ManualStart();
    bool CheckPermission(const std::string &permissionName);
    void SendGetFrameResEvent(const bool isGetSucc, const int32_t sizeRead);
    

private:
    int32_t buffer_size = 1024*1024; //default buffersize 1MB
    std::map<std::string, SANE_Handle> scannerHandleList_;

    ServiceRunningState state_;
    static std::mutex instanceLock_;
    static sptr<ScanServiceAbility> instance_;
    static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
    static std::map<std::string, sptr<IScanCallback>> registeredListeners_;
    
    std::recursive_mutex apiMutex_;

    std::mutex lock_;
    uint64_t currentJobId_;
};
} // namespace OHOS::Scan
#endif // SCAN_SYSTEM_ABILITY_H


